/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.base.graph
 * author:David
 * Date:2015年8月21日上午11:36:06
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.graph;

import com.xuwei.base.link.Node;

/**
 * 双链式存储结构的顶点定义
 * @author David
 * @since 2015年8月21日 上午11:36:06
 * @version 
 * @since JDK 1.6
 */
public class Vertex {
	/*顶点信息*/
	private Object info;
	/*顶点的邻接边表*/
	private LinkList adjacentEdges;
	/*顶点的逆邻接边表，若是无向图，则为空*/
	private LinkList reAdjacentEdges;
	/*访问状态*/
	private boolean visited;
	/*顶点在顶点表中的位置*/
	private Node vexPosition;
	/*顶点所在图的类型*/
	private int graphType;
	/*应用。如求最短路径时为Path，求关键路径时为Vtime*/
	private Object application;
	
	/*在图G中引入一个新顶点*/
	public Vertex(Graph g,Object info) {
		this.info=info;
		adjacentEdges=new LinkListDLNode();
		reAdjacentEdges=new LinkListDLNode();
		visited=false;
		graphType=g.getType();
		vexPosition=g.insert(this);
		application=null;
	}
	
	//辅助方法：判断顶点所在图的类型
	private boolean isUnDiGraphNode(){
		return graphType==Graph.UndirectedGraph;
	}

	//获取或设置顶点信息
	public Object getInfo(){return info;}
	public void setInfo(Object obj){this.info=info;}
	
	//与顶点的度相关的方法
	public int getDeg(){
		if(isUnDiGraphNode()){
			//无向图顶点的（出/入）度为邻接边表规模
			return adjacentEdges.getSize();
		}else{
			//有向图顶点的度为出度与入度之和
			return getOutDeg()+getInDeg();
		}
	}
	
	public int getOutDeg(){
		//有（无）向图顶点的出度为邻接表规模
		return adjacentEdges.getSize();
	}
	
	public int getInDeg(){
		if(isUnDiGraphNode()){
			//无向图顶点的入度就是它的度
			return adjacentEdges.getSize();
		}else{
			//有向图顶点入度为逆邻接表的规模
			return reAdjacentEdges.getSize();
		}
	}
	
	/**
	 * 获取与顶点相关联的边
	 * @return
	 */
	public LinkList getAdjacentEdges(){
		return adjacentEdges;
	}
	public LinkList getReAdjacentEdges(){
		if(isUnDiGraphNode()){
			//无向图顶点的逆邻接边表就是其邻接边表
			return adjacentEdges;
		}else{
			return reAdjacentEdges;
		}
	}
	
	//取顶点在所属图顶点集中的位置
	public Node getVexPosition(){
		return vexPosition;
	}
	
	//与顶点访问状态相关方法
	public boolean isVisited(){
		return visited;
	}
	
	public void setToVisited(){
		visited=true;
	}
	
	public void setToUnvisited(){
		visited=false;
	}
	
	//取顶点应用信息
	protected Object getAppObj(){
		return application;
	}
	//设置顶点应用信息
	protected void setAppObj(Object app){
		application = app;
	}
	
	//重置顶点状态信息
	public void resetStatus(){
		visited=false;
		application=null;
	}
	
	
}
